package com.example.myshools;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.myshools.Activities.ActivitiesFragment;
import com.example.myshools.Consult.ConsultFragemnt;
import com.example.myshools.Mine.organization.MineFragment;
import com.example.myshools.Mine.student.StudentFragment;

import java.util.HashMap;
import java.util.Map;

public class MainActivity extends AppCompatActivity {
        private class MyTabSpec {
            private ImageView imageView = null;
            private TextView textView = null;
            private int normalImage;
            private int selectImage;
            private Fragment fragment = null;
            private void setSelect(boolean b) {
                if (b) {
                    imageView.setImageResource(selectImage);
                    textView.setTextColor(
                            Color.parseColor("#00FF00"));
                } else {
                    imageView.setImageResource(normalImage);
                    textView.setTextColor(
                            Color.parseColor("#000000"));
                }
            }

            public ImageView getImageView() {
                return imageView;
            }

            public void setImageView(ImageView imageView) {
                this.imageView = imageView;
            }

            public TextView getTextView() {
                return textView;
            }

            public void setTextView(TextView textView) {
                this.textView = textView;
            }

            public int getNormalImage() {
                return normalImage;
            }

            public void setNormalImage(int normalImage) {
                this.normalImage = normalImage;
            }

            public int getSelectImage() {
                return selectImage;
            }

            public void setSelectImage(int selectImage) {
                this.selectImage = selectImage;
            }

            public Fragment getFragment() {
                return fragment;
            }

            public void setFragment(Fragment fragment) {
                this.fragment = fragment;
            }
        }

        private Map<String, MyTabSpec> map = new HashMap<>();
        private String[] tabStrId = {"活动", "咨询", "我的"};
        private Fragment curFragment = null;


    @Override
    protected void onCreate (Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            getWindow().setStatusBarColor(0xff7adfb8);
        }
        initData();
        setListener();

        /*登录选择角色的时候进行
        * */
        SharedPreferences sharedPreferences=getSharedPreferences("role",MODE_PRIVATE);
        SharedPreferences.Editor ed = sharedPreferences.edit();
        ed.putInt("role",0);
        ed.commit();

        //用户信息登录的时候设置
        SharedPreferences sharedPreferences1=getSharedPreferences("user",MODE_PRIVATE);
        SharedPreferences.Editor ed1 = sharedPreferences1.edit();
        ed1.putInt("userId",1);
        ed1.putInt("isIdentity",1);
        ed1.commit();
        changeTab(tabStrId[0]);
    }
        private class MyListener implements View.OnClickListener {
            @Override
            public void onClick(View v) {
                switch (v.getId()) {
                    case R.id.tab_spec_1:
                        changeTab(tabStrId[0]);
                        break;
                    case R.id.tab_spec_2:
                        changeTab(tabStrId[1]);
                        break;
                    case R.id.tab_spec_3:
                        changeTab(tabStrId[2]);
                        break;
                }
            }
        }

        private void changeTab (String s){
            changeFragment(s);
            changeImage(s);
            setImage();
        }

        private void changeFragment (String s){
            Fragment fragment = map.get(s).getFragment();
            if (curFragment == fragment) return;
            FragmentTransaction transaction =
                    getSupportFragmentManager().beginTransaction();
            if (curFragment != null)
                transaction.remove(curFragment);

            if (!fragment.isAdded()) {
                transaction.add(R.id.tab_content, fragment);
            }

            transaction.show(fragment);
            curFragment = fragment;

            transaction.commit();
        }

        private void changeImage (String s){
            for (String key : map.keySet()) {
                map.get(key).setSelect(false);
            }
            map.get(s).setSelect(true);
        }
    private void setImage() {
        map.get(tabStrId[0]).setNormalImage(R.drawable.huodong);
        map.get(tabStrId[0]).setSelectImage(R.drawable.huodong);
        map.get(tabStrId[1]).setNormalImage(R.drawable.xixun);
        map.get(tabStrId[1]).setSelectImage(R.drawable.xixun);
        map.get(tabStrId[2]).setNormalImage(R.drawable.gerenzhongxin);
        map.get(tabStrId[2]).setSelectImage(R.drawable.gerenzhongxin);

    }

        private void setListener () {
            LinearLayout layout1 = findViewById(R.id.tab_spec_1);
            LinearLayout layout2 = findViewById(R.id.tab_spec_2);
            LinearLayout layout3 = findViewById(R.id.tab_spec_3);
            MyListener listener = new MyListener();
            layout1.setOnClickListener(listener);
            layout2.setOnClickListener(listener);
            layout3.setOnClickListener(listener);
        }

        private void initData () {
            Log.e("ww","初始化页面");
            map.put(tabStrId[0], new MyTabSpec());
            map.put(tabStrId[1], new MyTabSpec());
            map.put(tabStrId[2], new MyTabSpec());
            setFragment();
            findView();
        }
        private void setFragment () {
            int role=getSharedPreferences("role",MODE_PRIVATE).getInt("role",-1);
            Log.e("role",role+"");
            map.get(tabStrId[0]).setFragment(new ActivitiesFragment());
            map.get(tabStrId[1]).setFragment(new ConsultFragemnt());
            if(role==0) {
                Log.e("ww","组织");
                map.get(tabStrId[2]).setFragment(new MineFragment());
            }
            if(role==1){
                Log.e("ww","学生");
                map.get(tabStrId[2]).setFragment(new StudentFragment());
            }
        }

        private void findView () {
            ImageView iv1 = findViewById(R.id.img_1);
            ImageView iv2 = findViewById(R.id.img_2);
            ImageView iv3 = findViewById(R.id.img_3);
            TextView tv1 = findViewById(R.id.tv_1);
            TextView tv2 = findViewById(R.id.tv_2);
            TextView tv3 = findViewById(R.id.tv_3);
            map.get(tabStrId[0]).setImageView(iv1);
            map.get(tabStrId[0]).setTextView(tv1);
            map.get(tabStrId[1]).setImageView(iv2);
            map.get(tabStrId[1]).setTextView(tv2);
            map.get(tabStrId[2]).setImageView(iv3);
            map.get(tabStrId[2]).setTextView(tv3);
        }
    }

